//
//  NKFeedbackVC.m
//  NewKY
//
//  Created by admin on 16/11/14.
//  Copyright © 2016年 Dee. All rights reserved.
//

#import "NKFeedbackVC.h"
#import "CZAdditions.h"
#define MAX_LIMIT_NUMS 20

@interface NKFeedbackVC ()<UITextViewDelegate>

/*---- text -----*/
@property(nonatomic,strong)UITextView *textView;
/*---- 占位label -----*/
@property(nonatomic,strong)UILabel *placeholdeLabel;

@property(nonatomic,strong)UIView *PhoneView;
/*---- 显示剩余输入 -----*/
@property(nonatomic,strong)UILabel *wordCountLabel;
/*---- 进度显示 -----*/
@property(nonatomic,strong)UIActivityIndicatorView *activityIndicatorView;


@end

@implementation NKFeedbackVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}
 /*---- 提交按钮点击事件 -----*/
-(void)buttonAction:(UIBarButtonItem *)button{
    
    if (self.textView.text.length == 0) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"内容不能为空" message:nil preferredStyle:UIAlertControllerStyleAlert];
        
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:NULL]];
        
        [self presentViewController:alert animated:YES completion:nil];
        
    }else{
        
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(myMethod) userInfo:nil repeats:NO];
        
        [SVProgressHUD showProgress:-1 status:@"正在提交..."];
   }

}

 /*---- 弹框延迟 -----*/
-(void)myMethod{
    [SVProgressHUD showSuccessWithStatus:@"感谢您的提议,我们将尽快回复"];
   [NSTimer scheduledTimerWithTimeInterval:2 repeats:NO block:^(NSTimer * _Nonnull timer) {
       
       [self.navigationController popViewControllerAnimated:YES];
   }];
}

#pragma mark 搭建界面
-(void)setupUI{
    /*---- 设置导航控制器按钮 -----*/
    UIBarButtonItem *button = [[ UIBarButtonItem alloc]initWithTitle:@"提交" style:UIBarButtonItemStylePlain target:self action:@selector(buttonAction:)];
    button.tintColor = [UIColor whiteColor];
    self.navigationItem.rightBarButtonItem = button;
    
    //================== 设置背景图片=================//
    UIImage *img = [UIImage imageNamed:@"An_Background"];
    UIImageView *imgV = [[UIImageView alloc]initWithImage:img];
    imgV.frame = self.view.bounds;
    [self.view addSubview:imgV];
    
     /*---- 添加意见反馈表 -----*/
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height * 0.5 - 50)];
     /*---- 设置textView字体大小 -----*/
    textView.font = [UIFont fontWithName:@"Arial" size:18.0];
    textView.backgroundColor = [UIColor clearColor];
    self.textView = textView;
    
    // 设置从顶部开始输入
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:textView];
    textView.delegate = self;
    
     /*---- 设置label -----*/
    _placeholdeLabel = [[UILabel alloc] initWithFrame:CGRectMake(3, 3, [UIScreen mainScreen].bounds.size.height, 50)];
    _placeholdeLabel.font = [UIFont systemFontOfSize:18];
    _placeholdeLabel.textColor = [UIColor lightGrayColor];
    _placeholdeLabel.text = @"请输入您对我们的宝贵意见";
    _placeholdeLabel.numberOfLines = 0;
    _placeholdeLabel.enabled = NO;
    [self.textView addSubview:_placeholdeLabel];
    
     /*---- 右下角显示剩余输入label -----*/
    UILabel *wordCountLabel = [[UILabel alloc]initWithFrame:CGRectMake(255, 260, 150, 20)];
    
    wordCountLabel.font = [UIFont systemFontOfSize:16];
    wordCountLabel.textColor = [UIColor lightGrayColor];
    wordCountLabel.enabled = NO;
    wordCountLabel.text = @"还可以输入20位";
    [self.textView addSubview:wordCountLabel];
    self.wordCountLabel = wordCountLabel;
    
     /*---- 弹出键盘 -----*/
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];
    
    [topView setItems:buttonsArray];
    [textView setInputAccessoryView:topView];
    
     /*---- 联系栏 -----*/
    UILabel *label = [[UILabel alloc]init];
    label.text = @"\t欢迎联系我们";
    label.backgroundColor = [UIColor cz_colorWithHex:0x51B4B1];
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:15];
    CGFloat labelH = 30;
    [self.view addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.view);
        make.height.mas_equalTo(labelH);
        make.left.right.mas_equalTo(self.view);
    }];
    
     /*---- cell 栏 -----*/
    CGFloat phoneViewH = 88;
    UIView *phoneView = [[UIView alloc]initWithFrame:CGRectMake(0, self.view.bounds.size.height * 0.5 - 20, self.view.bounds.size.width, phoneViewH)];

    [self.view addSubview:phoneView];
    self.PhoneView = phoneView;
     /*---- 电话图标 -----*/
    UIImage *phoneImg = [UIImage imageNamed:@"medtronic_phone"];
    UIImageView *phoneImgV = [[UIImageView alloc]initWithImage:phoneImg];
    [self.PhoneView addSubview:phoneImgV];
    UIImage *userExchangeImg = [UIImage imageNamed:@"medtronic_story"];
    UIImageView *userExchangeImgV = [[UIImageView alloc]initWithImage:userExchangeImg];
    [self.PhoneView addSubview:userExchangeImgV];
    
      /*----  提示文字  ---- */
    UIButton *phoneBtn = [[UIButton alloc]init];
    [phoneBtn setTitle:@"联系我们" forState:UIControlStateNormal]
    ;

    [phoneBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [phoneBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    
    phoneBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.PhoneView addSubview:phoneBtn];
    
    UIButton *UserBtn = [[UIButton alloc]init];
    [UserBtn setTitle:@"用户交流群" forState:UIControlStateNormal];
    [UserBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [UserBtn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
    UserBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [self.PhoneView addSubview:UserBtn];
    
     /*---- 底部view -----*/
    UIView *phoneLineView = [[UIView alloc]init];
    phoneLineView.backgroundColor = [UIColor blackColor];
    [self.PhoneView addSubview:phoneLineView];
    
    UIView *userLineView = [[UIView alloc]init];
    userLineView.backgroundColor = [UIColor blackColor];
    [self.PhoneView addSubview:userLineView];
    
     /*---- 箭头 -----*/
    UIImage *arrowImg = [UIImage imageNamed:@"HL_leftBtn"];
    UIImageView *arrowImgV = [[UIImageView alloc]initWithImage:arrowImg];
    [self.PhoneView addSubview:arrowImgV];
    
    UIImage *arrowIcon = [UIImage imageNamed:@"HL_leftBtn"];
    UIImageView *arrowIconV = [[UIImageView alloc]initWithImage:arrowIcon];
    [self.PhoneView addSubview:arrowIconV];
    
    //================== 约束 =================//
    CGFloat phoneImgVHW = 25;
    [phoneImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.PhoneView).offset(20);
        make.height.width.mas_equalTo(phoneImgVHW);
        make.top.mas_equalTo(self.PhoneView).offset(12);
        
    }];
    
    [userExchangeImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(phoneImgV.mas_bottom).offset(15);
        make.left.mas_equalTo(phoneImgV.mas_left);
        make.height.width.mas_equalTo(phoneImgVHW);

    }];
    
    [phoneBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneImgV.mas_right).offset(10);
        make.top.mas_equalTo(self.PhoneView).offset(10);
        make.width.mas_equalTo(85);
        make.height.mas_equalTo(30);
    }];
    
    [UserBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneBtn);
        make.bottom.mas_equalTo(self.PhoneView).offset(-10);
        make.width.mas_equalTo(100);
        make.height.mas_equalTo(30);
        
    }];
    
    [phoneLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneBtn);
        make.bottom.mas_equalTo(phoneBtn.mas_bottom).offset(8);
        make.right.mas_equalTo(self.PhoneView);
        make.height.mas_equalTo(0.5);
        
    }];
    
    [userLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(phoneLineView);
        make.right.mas_equalTo(self.PhoneView);
        make.height.mas_equalTo(0.5);
        make.bottom.mas_equalTo(self.PhoneView.mas_bottom).offset(-2);
    }];
    
    [arrowImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(phoneBtn.mas_centerY);
        make.width.height.mas_equalTo(10);
        make.right.mas_equalTo(-20);
    }];
    
    [arrowIconV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(UserBtn.mas_centerY);
        make.width.height.mas_equalTo(10);
        make.right.mas_equalTo(-20);
    }];


}

#pragma mark - UITextView 的代理方法
- (void)textViewDidChange:(UITextView *)textView{
    
    if (_textView.text.length != 0) {
        
        _placeholdeLabel.hidden = YES;
        
    }else{
       //为了保证在删除所输入的内容的情况下，提示符再次出现
        _placeholdeLabel.hidden = NO;
    }
   /*---- 添加右下角的输入限制 -----*/
    unsigned long count;
    count = MAX_LIMIT_NUMS - textView.text.length;
    [self.wordCountLabel setText:[NSString stringWithFormat:@"还可以输入%lu位",count]];
    if(count == MAX_LIMIT_NUMS){
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"友情提示" message:@"最多只能输入20字" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:NULL]];
        [self presentViewController:alert animated:YES completion:nil];
    }
}


-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    NSString *comcatstr = [textView.text stringByReplacingCharactersInRange:range withString:text];
    
    NSInteger caninputlen = MAX_LIMIT_NUMS - comcatstr.length;
    if (caninputlen >= 0 ) {
        return  YES;
    }
    else
    {
        NSInteger len = text.length + caninputlen;
        NSRange rg = {0 ,MAX(len,0)};
        
        if (rg.length > 0) {
            NSString *s = [text substringWithRange:rg];
            [textView setText:[textView.text stringByReplacingCharactersInRange:range withString:s]];
        }
        return NO;
    }
    
}

//关闭键盘
-(void) dismissKeyBoard{
    
    [self.textView resignFirstResponder];
}

 /*---- 点击空白处收回键盘 -----*/
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
 
    [self.textView resignFirstResponder];
    
}


 /*---- 设置导航栏标题 -----*/
-(void)setTitleName:(NSString *)titleName{
    _titleName = titleName;
    
    self.title = titleName;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
